<?php
class Admin_client extends Client_Controller{
	function __construct(){
		parent :: __construct();
		$this->load->model("Client_m");
		$this->_data['sidebar'] = "client";
	}
	
	function index(){
		$this->clients();	
	}
	
	//--list all clients--//
	function clients(){
		is_admin();
		$this->_data['page_title'] = "Clients";
		$per_page = $this->uri->segment(3);
		$off_set = $this->uri->segment(4);
		if($per_page == ""){
			$per_page = 30;
		}
		if($off_set == ""){
			$off_set = "";
		}
		$config['base_url'] = base_url()."index.php/admin_client/clients/".$per_page."/";
		$config['per_page'] = $per_page;
		$config['uri_segment'] = 4;
		$config['total_rows'] = $this->Client_m->count_clients();
		if($off_set > 0 && $off_set == $config['total_rows']){
			$off_set = $off_set - $per_page;
		}
		$this->_data['client_data'] = $this->Client_m->list_all_clients($per_page, $off_set)->result_array();
		$this->pagination->initialize($config);
    	$this->_data['pagination'] = $this->pagination->create_links();
    	$this->_data['total'] = $config['total_rows'];
    	$this->_data['perpage'] = $per_page;
    	$this->_data['offset'] = $off_set;
    	$this->display_admin('admin/client/list_clients');
	}
	
	//--add clients--//
	function add_client(){
		is_admin();
		$this->_data['page_title'] = "Ajouter Client";
		$config = array(
							array("field" => "client_name", "label" => " nom du client",
								  "rules"=>"required|xss_clean|trim|callback_check_exist_client_name"
								  )
		
				       );
		$this->form_validation->set_rules($config);
		if($this->input->post("add_client")){
			if($this->form_validation->run() === FALSE){
				$this->display_admin("admin/client/add_client");		
			}else{
				$data = array("name" => $this->input->post("client_name"));
				if($this->Client_m->add_client($data)){
					$this->session->set_flashdata('error', lang('add') ." ". lang('success'));
    				redirect(site_url('admin_client'));	
				}else{
					$this->_data['insert_error'] = "Insert failed!";
				}
				
			}
		}
		$this->display_admin("admin/client/add_client");
	}
	
	//--check exist client name--//
	function check_exist_client_name($client_name){
		$clients = $this->Client_m->count_client_by_name($client_name);
		if($clients>0){
			$this->form_validation->set_message("check_exist_client_name", "le nom du client existe déjà dans le système");
			return FALSE;
		}else{
			return TRUE;
		}
	}
	
	//--edit client--//
	function edit_client($client_id = null){
		is_admin();
		$this->_data['page_title'] = lang('edit_profile')." "."client";
		if($this->Client_m->get_client_details($client_id) === FALSE){
			redirect("admin_client");
		}
		$client_data = $this->Client_m->get_client_details($client_id);
		$this->_data['clients'] = $client_data;
		$this->_data['client_name'] = $client_data->name;
		$this->_data['client_id'] = $client_data->id;	
		$config = array(
							array("field" => "client_name", 
								  "label" => "nom du client",
								  "rules" => "required|xss_clean|trim"
								 )
		
				       );
		$this->form_validation->set_rules($config);
		
		if($this->input->post('edit_client')){
			if($this->form_validation->run()){
				$data = array("name"=>$this->input->post("client_name"));
				if($this->Client_m->edit_client($client_id, $data)){
					$this->session->set_flashdata('error', lang('edit_profile') ." ". lang('success'));
					redirect('admin_client/clients');	
				}	
			}else{
				$this->display_admin('admin/client/edit_client');
			}			
		}else{
			
			$this->display_admin('admin/client/edit_client');
		}
		
			
	}
	
	//--delete client--//
	function delete_client($id){
		is_admin();
		$delete = $this->Client_m->delete_client($id);
		if($delete){
			$this->session->set_flashdata('error', lang('delete')." ".lang('success'));
			redirect('admin_client/clients');
		}else{
			$this->session->set_flashdata('error', lang('delete')." ".lang('fail'));
			redirect('admin_client/clients');
		}
	}
	
	//--delete multi contact--//
	function delete_multi_clients(){
		is_admin();
        $list = "";
        $adelete = $this->input->post('delete');
        $N = count($adelete);
        for ($i = 0; $i < $N; $i++) {
            
            if ($adelete[$i] != "")
                $list = $list . ',' . $adelete[$i];
        }
        $list = '(' . substr($list, 1) . ')';

        $query = $this->Client_m->delete_multi_clients($list);
        if ($query)
            $this->session->set_flashdata('error', lang('delete') ." ". lang('success'));
        else
            $this->session->set_flashdata('error', lang('delete') ." ". lang('fail'));
        redirect('admin_client/clients');
	}
	
	//--list client images--//
	function list_client_images(){
		is_admin();
		$this->_data['page_title'] = "Client images";
		$per_page = $this->uri->segment(3);
		$off_set = $this->uri->segment(4);
		if($per_page == ""){
			$per_page = 20;
		}
		if($off_set == ""){
			$off_set = "";
		}
		$config['base_url'] = base_url()."index.php/admin_client/list_client_images/".$per_page."/";
		$config['per_page'] = $per_page;
		$config['uri_segment'] = 4;
		$config['total_rows'] = $this->Client_m->count_client_images();
		if($off_set > 0 && $off_set == $config['total_rows']){
			$off_set = $off_set - $per_page;
		}
		$this->_data['client_images'] = $this->Client_m->list_all_client_images($per_page, $off_set)->result_array();
		$this->pagination->initialize($config);
    	$this->_data['pagination'] = $this->pagination->create_links();
    	$this->_data['total'] = $config['total_rows'];
    	$this->_data['perpage'] = $per_page;
    	$this->_data['offset'] = $off_set;
    	$this->display_admin('admin/client/list_client_images');	
	}
	
	//--upload client images--//
	function add_client_image(){
		is_admin();
		$this->_data['page_title']=lang('add')." Client image";
		
		$config = array(
           	 array(
                'field' => 'title',
                'label' => lang('title'),
                'rules' => 'required'),
           	 
            array(
                'field' => 'userfile',
                'label' => 'Image',
                'rules' => 'callback__do_upload'
            )
           
           
        );
        $this->form_validation->set_rules($config);
        if ($this->input->post('add_client_image')) {
        	if($this->form_validation->run() === FALSE){
        		$this->display_admin("admin/client/add_client_image");		
        	}else{
        	
        			$upload_info = $this->upload->data();
                	$thumb_path = $upload_info['file_path'].'thumbs/';
        			
	              	$config = array(
		                        'source_image' => $upload_info['full_path'], //get original image
		                        'new_image' => $thumb_path,//save as new image //need to create thumbs first
		                        'maintain_ratio' => true,
		                        'width' => 300,
		                        'height' => 200
	                    		);
	                 $this->load->library('image_lib', $config); //load library
	                 $this->image_lib->resize(); //do whatever specified in config
	                    
		       		 $data = array(
	                        'title' => $this->input->post('title'),
	                        'image_name' => $upload_info['file_name']
	                     
	                    );
	                 $query = $this->Client_m->add_image($data);
	                 if($query){
	                 	$this->session->set_flashdata("error", lang("add"). " ". lang("success"));
	                 	redirect("admin_client/list_client_images");
	                 }
	        		
        	}    
          
	    	
        	
        }else{
        	$this->display_admin("admin/client/add_client_image");
	    	}  	
	}
	
	//--validate image upload--//
	function _do_upload($file = null)
	{
	    $image_path = './assets/client_img';
        $thumb_path = $image_path . '/thumbs';
	    $config = array(
                    'allowed_types' => "jpg|jpeg|gif|png",
                    'upload_path' => $image_path,
                    'max_size' => 2000,
                    'encrypt_name' => true
         );
        
	    $this->load->library('upload', $config);
	        
	    if ( ! $this->upload->do_upload())
	    {
	        // set the validation error using the errors provided by the upload class
	        $this->form_validation->set_message('_do_upload', $this->upload->display_errors());
	        return FALSE;
	    }    
	    else
	    {
	        return TRUE;
	    }
	}
	
	//--edit client image--//
	function edit_client_image($client_id = null){
		is_admin();
		$this->_data['page_title'] = lang('edit_profile')." "."client image";
		if($this->Client_m->get_client_image_details($client_id) === FALSE){
			redirect("admin_client/list_client_images");
		}
		$client_data = $this->Client_m->get_client_image_details($client_id);
		$this->_data['client_image'] = $client_data;
		$this->_data['image_name'] = $client_data->image_name;
		$this->_data['image_id'] = $client_data->id;	
		$this->_data['image_title'] = $client_data->title;	
		$config = array(
							array("field" => "title", 
								  "label" => lang('title'),
								  "rules" => "required|xss_clean|trim"
								 )
		
				       );
		$this->form_validation->set_rules($config);
		
		if($this->input->post('edit_client_image')){
			if($this->form_validation->run()){
				$data = array("title"=>$this->input->post("title"));
				if($this->Client_m->edit_client_image($client_id, $data)){
					$this->session->set_flashdata('error', lang('edit_profile') ." ". lang('success'));
					redirect('admin_client/list_client_images');	
				}	
			}else{
				$this->display_admin("admin/client/edit_client_image");
			}			
		}else{
			
			$this->display_admin("admin/client/edit_client_image");
		}
		
			
	}

	//--delete client image--//
	function delete_client_image($id){
		is_admin();
		$query = $this->Client_m->get_client_image_details($id); 
        if (count($query)>=0) {
           $info = $query;
           if ($info->image_name != null) {
                   $image_to_delete = './assets/client_img/' . $info->image_name;
                   $thumb_to_delete = './assets/client_img/thumbs/' . $info->image_name;
                   delete_images($image_to_delete, $thumb_to_delete);
           }
        }
		$delete = $this->Client_m->delete_client_image($id);
		if($delete){
			$this->session->set_flashdata('error', lang('delete')." ".lang('success'));
			redirect('admin_client/list_client_images');
		}else{
			$this->session->set_flashdata('error', lang('delete')." ".lang('fail'));
			redirect('admin_client/list_client_images');
		}
	}
	
	//--delete multi clien images--//
	function delete_multi_client_images(){
		is_admin();
        $list = "";
        $adelete = $this->input->post('delete');
        $N = count($adelete);
        for ($i = 0; $i < $N; $i++) {
            $image = $this->Client_m->get_client_image_details($adelete[$i]);
         
                $image_to_delete = './assets/client_img/' . $image->image_name;
                $thumbs_name = './assets/client_img/thumbs/' . $image->image_name;
                if ($image->image_name != null) {
                    delete_images($image_to_delete, $thumbs_name);
                }
            
            if ($adelete[$i] != "")
                $list = $list . ',' . $adelete[$i];
        }
        $list = '(' . substr($list, 1) . ')';

        $query = $this->Client_m->delete_multi_client_images($list);
        if ($query)
            $this->session->set_flashdata('error', lang('delete').' ' . lang('success'));
        else
            $this->session->set_flashdata('error', lang('delete').' ' . lang('fail'));
        redirect('admin_client/list_client_images');
	}
	
				
}